package com.one.blocks.rbac.controller;

import com.one.blocks.mvc.request.PageRequest;
import com.one.blocks.mvc.response.WebPageResponse;
import com.one.blocks.mvc.response.WebResponse;
import com.one.blocks.mvc.controller.valid.Update;
import com.one.blocks.mvc.controller.AbstractController;
import com.one.blocks.mvc.controller.valid.UpdateSelective;
import com.one.blocks.security.annotation.PreAuthorize;
import com.one.blocks.rbac.controller.query.SysDictTypeQuery;
import com.one.blocks.rbac.controller.vo.SysDictTypeVo;
import com.one.blocks.rbac.controller.convert.SysDictTypeConvert;
import com.one.blocks.rbac.domain.SysDictType;
import com.one.blocks.rbac.manager.SysDictTypeManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2023-01-24 10:56:30
 */
@Api(tags = "字典类型接口")
@RestController
@RequestMapping("/system/dictType")
@Slf4j
@Validated
public class SysDictTypeController extends AbstractController<SysDictTypeManager, SysDictTypeConvert, SysDictType, SysDictTypeVo, SysDictTypeQuery> {

    @Autowired
    public SysDictTypeController(SysDictTypeManager manager) {
        super(manager, SysDictTypeConvert.INSTANCE);
    }

    @ApiOperation("查询单个字典类型")
    @PreAuthorize("system:dictType:view")
    @GetMapping
    public WebResponse<SysDictTypeVo> get(@Validated @NotNull @RequestParam Integer id) {
        return super.generalGet(id);
    }

    @ApiOperation("根据id批量查询字典类型")
    @PreAuthorize("system:dictType:view")
    @GetMapping("/batch")
    public WebResponse<List<SysDictTypeVo>> getByIds(@Validated @NotNull @Size(min = 1, max = 100) @RequestParam Set<Integer> ids) {
        return super.generalGetIds(ids);
    }

    @ApiOperation("分页查询字典类型")
    @PreAuthorize("system:dictType:view")
    @GetMapping("/page")
    public WebPageResponse<SysDictTypeVo> page(@Validated PageRequest request, @Validated SysDictTypeQuery query) {
        return super.generalPage(request, query);
    }

    @ApiOperation("添加字典类型")
    @PreAuthorize("system:dictType:insert")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Serializable> insert(@Validated @RequestBody SysDictTypeVo sysDictTypeVo) {
        return super.generalInsert(sysDictTypeVo);
    }

    @ApiOperation("修改字典类型")
    @PreAuthorize({"system:dictType:update"})
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> update(@Validated(Update.class) @RequestBody SysDictTypeVo sysDictTypeVo) {
        return super.generalUpdate(sysDictTypeVo);
    }

    @ApiOperation("根据id批量删除字典类型")
    @PreAuthorize({"system:dictType:update"})
    @PatchMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> updateSelective(@Validated(UpdateSelective.class) @RequestBody SysDictTypeVo sysDictTypeVo) {
        return super.generalUpdateSelective(sysDictTypeVo);
    }

    @ApiOperation("删除字典类型")
    @PreAuthorize("system:dictType:delete")
    @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Integer> delete(@Validated @NotNull @Size(min = 1, max = 100) @RequestBody Set<Integer> ids) {
        return super.generalDeleteByIds(ids);
    }
}